//
//  DigitalWithdrawViewController.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/8/13.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "DigitalWithdrawViewController.h"
#import "ChangeVerifyView.h"
#import "HYNetHelper.h"
#import "UserCryptoAddress.h"
#import <YYModel.h>
#import "SheetView.h"
#import "IncomingDetailsViewController.h"
#import "BankListTableViewController.h"

@interface DigitalWithdrawViewController ()

@property (weak, nonatomic) IBOutlet UITextField *address;
@property (weak, nonatomic) IBOutlet UITextField *amountTextField;
@property (nonatomic, strong) NSArray *addressList;
@property (weak, nonatomic) IBOutlet UILabel *availableValue;
@property (weak, nonatomic) IBOutlet UILabel *message;
@property (weak, nonatomic) IBOutlet UILabel *amountTitle;
@property (weak, nonatomic) IBOutlet UILabel *availableTitle;
@property (weak, nonatomic) IBOutlet UIButton *withdrawButton;
@property (weak, nonatomic) IBOutlet UILabel *tipsTitle;

@end

@implementation DigitalWithdrawViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self requestUserCryptoAddress];
    
    self.availableValue.text = self.balance.available_balance_display;
    
    self.title = [NSString stringWithFormat:@"%@ %@", JFMainLocalizedString(@"withdraw.withdraw"), self.balance.asset_symbol];
    self.tipsTitle.text = JFMainLocalizedString(@"withdraw.tips");
    self.message.text = JFMainLocalizedString(@"withdraw.message3");
    self.availableTitle.text = JFMainLocalizedString(@"withdraw.available");
    self.amountTitle.text = JFMainLocalizedString(@"withdraw.withdraw-amount");
    self.amountTextField.placeholder = JFMainLocalizedString(@"withdraw.please-enter-amount");
    self.address.placeholder = JFMainLocalizedString(@"withdraw.address");
    [self.withdrawButton setTitle:JFMainLocalizedString(@"withdraw.withdraw") forState:UIControlStateNormal];
}

- (void)requestUserCryptoAddress {
    WEAKSELF
    [HYNetHelper userCryptoAddress:^(JFResultVo *resultVo) {
        if (!resultVo.errorNo) {
            weakSelf.addressList = [NSArray yy_modelArrayWithClass:[UserCryptoAddress class] json:resultVo.results[@"data"]];
        }
    }];
}

- (void)requestWithdraw:(NSString *)verifyType token:(NSString *)token {
    WEAKSELF
    [HYNetHelper send:self.balance.asset_symbol amount:self.amountTextField.text wallet_id:self.wallet_id to_wallet_id:nil to:self.address.text tenor:nil verifyType:verifyType token:token callBack:^(JFResultVo *resultVo) {
        if (!resultVo.errorNo)  {
            NSString *transfer_id = resultVo.results[@"transfer_id"];
            
            NSString *string = [NSString stringWithFormat:@"You have requested to withdraw %@ %@ successfully", weakSelf.amountTextField.text, weakSelf.balance.asset_symbol];
            NSRange range = [string rangeOfString:weakSelf.amountTextField.text];
            NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:string];
            [attStr addAttributes:@{NSForegroundColorAttributeName: UIColorFromHex(0xF8A828)} range:NSMakeRange(range.location, weakSelf.amountTextField.text.length + 4)];

            FZAlertController *alertController = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"alert_icon1"] title:JFMainLocalizedString(@"withdraw.withdraw-success") customView:nil message:string];
            alertController.messageAttStr = attStr;
            alertController.showCloseBtn = YES;
            alertController.messageAlignment = NSTextAlignmentCenter;
            alertController.closeClick = ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            };
            FZAlertAction *action = [FZAlertAction actionWithTitle:JFMainLocalizedString(@"deposit.see-details") style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
                IncomingDetailsViewController *vc = [[UIStoryboard storyboardWithName:@"User" bundle:nil]instantiateViewControllerWithIdentifier:@"IncomingDetailsViewController"];
                vc.transfer_id = transfer_id;
                [weakSelf.navigationController pushViewController:vc animated:YES];
                                }];
            [alertController addAction:action];
            [self presentViewController:alertController animated:YES completion:nil];
        }else {
            FZAlertController *alertController = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"alert_icon_failed"] title:@"Requested failed" customView:nil message:nil];
            alertController.showCloseBtn = YES;
            alertController.messageAlignment = NSTextAlignmentCenter;

            FZAlertAction *action = [FZAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
                                }];
            [alertController addAction:action];
            [self presentViewController:alertController animated:YES completion:nil];
        }
    }];
}

- (IBAction)withdrawAction:(id)sender {
    ChangeVerifyView *view = [[[NSBundle mainBundle] loadNibNamed:@"ChangeVerifyView" owner:nil options:nil] firstObject];
    view.frame = CGRectMake(0, 0, CUSTOMVIEW_WIDTH, 110);
    FZAlertController *vc = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"Captcha_ico_b"] title:nil customView:view message:nil];
    vc.showCloseBtn = YES;
    WEAKSELF
    FZAlertAction *action = [FZAlertAction actionWithTitle:JFMainLocalizedString(@"confirm") style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
        [weakSelf requestWithdraw:view.verifyType token:view.codeText.text];
                        }];
    [vc addAction:action];
    [self presentViewController:vc animated:YES completion:nil];
    
}

- (IBAction)selectCryptoAddress:(id)sender {
    BankListTableViewController *vc = [[UIStoryboard storyboardWithName:@"User" bundle:nil]instantiateViewControllerWithIdentifier:@"BankListTableViewController"];
    WEAKSELF
    vc.selectAddress = ^(CryptoAddressEntity * _Nonnull data) {
        weakSelf.address.text = data.wallet_address;
    };
    [self.navigationController pushViewController:vc animated:YES];
    
//    NSMutableArray *array = [NSMutableArray arrayWithCapacity:self.addressList.count];
//    for (UserCryptoAddress *address in self.addressList) {
//        [array addObject:address.wallet_address];
//    }
//    
//    SheetView *view = [[[NSBundle mainBundle] loadNibNamed:@"SheetView" owner:nil options:nil] firstObject];
//    view.frame = self.navigationController.view.bounds;
//    view.titleArray = array;
//    WEAKSELF
//    view.selectAction = ^(NSInteger index) {
//        weakSelf.address.text = array[index];
//    };
//    [self.navigationController.view addSubview:view];
}


@end
